﻿Imports System
Imports System.IO
Imports System.Data
Imports System.Text
Imports IDServer
Imports Infragistics

Public Class Frm_SYSModeDefine
    Implements IDServer.IDServer.IDServer


    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp
    Private mDataset As DataSet
    Private SelectNode As TreeNode
    Public bFind As Boolean
    Private Img As ImageList
    Private WithEvents MainForm As Frm_MainForm


    Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property



#Region "Interface Define"


    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        If AddEnable = False Then
            MessageBox.Show(My.resources.notallowadd, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End If

        ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd
        If SelectNode.Level = 2 Then
            MsgBox(Msg.NotAddNode, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Exit Function
        End If
        ClearText()
        MainForm.BtnEnable(False)
        SetCtlEnable(True)
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel
        Try
            ClearText()
            SetCtlEnable(False)
            If SelectNode.Tag.ToString = "All" Then
                bFind = False
            Else
                bFind = True
                FillText(SelectNode.Tag)
            End If
            MainForm.BtnEnable(bFind)
            GetToolEnable()
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
        Catch ex As Exception
            MsgBox("ActionCancel" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete
        Dim ID As String
        Try
            If DeleteEnable = False Then
                MessageBox.Show(My.Resources.NotAllowDelete, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            ID = SelectNode.Tag
            If MsgBox(My.resources.ConfirmDelete, MsgBoxStyle.Information + MsgBoxStyle.OkCancel, Msg.CompanyName) = MsgBoxResult.Ok Then
                If DeleteFromDataSet(ID) = True Then
                    MsgBox(My.resources.DeleteSuccess, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                End If
            End If
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            GetToolEnable()
        Catch ex As Exception
            MsgBox("ActionDelete:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        If EditEnable = False Then
            MessageBox.Show(My.resources.NotAllowEidt, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End If

        ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit
        MainForm.BtnEnable(False)
        SetCtlEnable(True)
        txtModeNo.Enabled = False
    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize
        Try
            Dim sql As String = ""
            Dim Ds As New DataSet
            If Me.txtModeNo.TextLength <= 0 Then
                MsgBox("編號不能為空！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "JLSoft")
                Me.txtModeNo.Focus()
                Return False
            End If
            If Me.txtModeName.TextLength <= 0 Then
                MsgBox("名稱不能為空！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "JLSoft")
                Me.txtModeName.Focus()
                Return False
            End If
            If Me.RBChild.Checked = True Then
                If Me.txtFormName.TextLength <= 0 Then
                    MsgBox("控件名稱不能為空！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "JLSoft")
                    Me.txtFormName.Focus()
                    Return False
                End If
            End If
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                sql = "select * from Sys_ModeDefine where ModeNo='" & Me.txtModeNo.Text & "'"
                Ds = ReturnDataset(sql)
                If Ds.Tables(0).Rows.Count > 0 Then
                    MsgBox("存在相同的編號！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "JLSoft")
                    Me.txtModeNo.Focus()
                    Me.txtModeNo.SelectAll()
                    Return False
                End If
                If SelectNode.Level = 3 Then
                    MsgBox("不能新增下級節點！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "JLSoft")
                    Return False
                End If
            End If
            Return True
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

        If BtnActionSave() = True Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region

#Region "Function Or sub Define"

    Private Sub LoadUTree(ByVal UTree As TreeView)
        Try
            Dim sql As String = ""
            Dim mNode As TreeNode
            mNode = New TreeNode
            Img = New ImageList()
            Dim Pictures As String = "35FLOPPY.BMP,Company.png,Company1.png,Depart.png,Depart1.png,Position.png,Position1.png"
            Dim Str() As String = Pictures.Split(",")
            Dim Company As String = Application.StartupPath & "\pic\35FLOPPY.BMP"
            Dim bumen As String = Application.StartupPath & "\pic\CLSDFOLD.BMP"
            Dim gangwei As String = Application.StartupPath & "\pic\OPENFOLD.BMP"
            Dim Image As Image = Nothing
            For i As Integer = 0 To Str.Length - 1
                Img.Images.Add(Drawing.Image.FromFile(Application.StartupPath & "\pic\" & Str(i)))
            Next
            'Img.Images.Add(Drawing.Image.FromFile(Company))
            'Img.Images.Add(Drawing.Image.FromFile(bumen))
            'Img.Images.Add(Drawing.Image.FromFile(gangwei))
            UTree.ImageList = Img
            mNode.Nodes.Clear()
            mNode.Tag = "All"
            mNode.Text = "全部"
            mNode.ImageIndex = 0
            UTree.Nodes.Add(mNode)
            sql = "select * from Sys_ModeDefine"
            mDataset = ReturnDataset(sql)
            LoadSecondTree(mNode, 0)
            SelectNode = mNode
            UTree.SelectedNode = SelectNode
            SelectNode.Expand()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub LoadSecondTree(ByVal Node As TreeNode, ByVal ParentID As Integer)
        Try
            If mDataset Is Nothing Then Exit Sub
            Dim mRow() As DataRow = mDataset.Tables(0).Select("ParentID=" & ParentID)

            If mRow.Length > 0 Then
                For Each Rows As DataRow In mRow
                    Dim Node1 As New TreeNode
                    If Node Is Nothing Then
                        Node1.Tag = Rows("ID")
                        Node1.Text = Rows("ModeName")
                        Node1.ImageIndex = 0
                        Node.Nodes.Add(Node1)
                        Node.Expand()
                        LoadSecondTree(Node1, Rows("ID"))
                    Else
                        Node1.Tag = Rows("ID")
                        Node1.Text = Rows("ModeName")
                        If ParentID = 0 Then
                            Node1.ImageIndex = 1
                            Node1.SelectedImageIndex = 2
                        Else
                            Node1.ImageIndex = 3
                            Node1.SelectedImageIndex = 4
                        End If
                        Node.Nodes.Add(Node1)
                        Node.Expand()
                        LoadSecondTree(Node1, Rows("ID"))
                    End If

                Next
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Function BtnActionSave() As Boolean
        Try
            Dim sql As String = ""
            Dim ID As String = ""
            Dim M_Ds As New DataSet
            Dim txtModeNo As String = Me.txtModeNo.Text
            Dim txtModeName As String = Me.txtModeName.Text
            Dim txtControlName As String = Me.txtFormName.Text
            Dim txtControlPicture As String = ""
            Dim txtRemark As String = Me.txtRemark.Text
            Dim ParentID As String = ""
            Dim mDecrypt As New IDSecurity
            txtControlPicture = mDecrypt.DataEncrypt(Me.txtFormPicture.Text).Replace("'", "^")
            If SelectNode.Tag.ToString = "All" Then
                ParentID = 0
            Else
                ParentID = SelectNode.Tag
            End If

            If ActionInitialize() = False Then Exit Function

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                sql = "Insert Into Sys_ModeDefine(ModeNo,ModeName,ControlName,ControlPicture,ParentID,Remark) values('" & _
                                 txtModeNo & "','" & txtModeName & "','" & txtControlName & "','" & txtControlPicture & "'," & ParentID & ",'" & txtRemark & "') ;"
                sql += " select @@IDENTITY As [ID]"
                M_Ds = ReturnDataset(sql)
                ID = M_Ds.Tables(0).Rows(0).Item(0)
                AddtoDataSet(ID, txtModeNo, txtModeName, txtControlName, txtControlPicture, ParentID, txtRemark)
                AddtoTree(SelectNode, ID, txtModeName)
            ElseIf ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                sql = "Update Sys_ModeDefine set ModeName='" & txtModeName & "',ControlName='" & txtControlName & "',ControlPicture='" & txtControlPicture & "',Remark='" & txtRemark & "' where ID=" & SelectNode.Tag
                ReturnObject(sql)
                EditToDataSet(txtModeName, txtControlName, txtControlPicture, txtRemark)
            End If
            MsgBox(IIf(ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd, My.resources.Addsuccess, My.resources.EditSuccess), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            SetCtlEnable(False)
            bFind = True
            MainForm.BtnEnable(bFind)
            GetToolEnable()
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            Return True
        Catch ex As Exception
            MsgBox("BtnActionSave:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Private Sub AddtoDataSet(ByVal ID As String, ByVal ModeNo As String, ByVal ModeName As String, ByVal ControlName As String, ByVal ControlPicture As String, ByVal ParentId As String, ByVal remark As String)
        Try
            Dim mRow As DataRow
            mRow = mDataset.Tables(0).NewRow
            mRow("ID") = ID
            mRow("ModeNo") = ModeNo
            mRow("modename") = ModeName
            mRow("controlname") = ControlName
            mRow("controlpicture") = ControlPicture
            mRow("parentid") = ParentId
            mRow("remark") = remark
            mDataset.Tables(0).Rows.Add(mRow)
            mDataset.AcceptChanges()
        Catch ex As Exception
            MsgBox("EditToDataSet:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub EditToDataSet(ByVal ModeName As String, ByVal ControlName As String, ByVal ControlPicture As String, ByVal Remark As String)
        Try
            If mDataset Is Nothing Then Exit Sub
            Dim Rows() As DataRow = mDataset.Tables(0).Select("ID=" & SelectNode.Tag)
            If Rows.Length > 0 Then
                For Each mRow As DataRow In Rows
                    mRow("modename") = ModeName
                    mRow("controlname") = ControlName
                    mRow("controlpicture") = ControlPicture
                    mRow("remark") = Remark
                Next
            End If
            mDataset.AcceptChanges()
        Catch ex As Exception
            MsgBox("AddtoDataSet:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Function DeleteFromDataSet(ByVal ID As String) As Boolean
        Dim sql As String
        Try
            sql = "Delete dbo.Sys_ModeDefine where id=" & ID
            ReturnObject(sql)
            Me.UTree.SelectedNode.Remove()
            Return True

        Catch ex As Exception
            MsgBox("DeleteFromDataSet:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Private Sub AddtoTree(ByVal TreeNode As TreeNode, ByVal NodeTag As String, ByVal NodeText As String)
        Try
            Dim mNode As New TreeNode
            mNode.Tag = NodeTag
            mNode.Text = NodeText
            mNode.ImageIndex = 1
            mNode.SelectedImageIndex = 2
            TreeNode.Nodes.Add(mNode)
            SelectNode = mNode
            SelectNode.Checked = True
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub ClearText()
        Try
            Me.txtModeNo.Text = ""
            Me.txtModeName.Text = ""
            Me.txtFormName.Text = ""
            Me.txtFormPicture.Text = ""
            Me.txtRemark.Text = ""
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub SetCtlEnable(ByVal B As Boolean)
        Try
            Me.txtModeNo.Enabled = B
            Me.txtFormName.Enabled = B
            Me.txtModeName.Enabled = B
            Me.txtFormPicture.Enabled = B
            Me.txtRemark.ReadOnly = Not B
            Me.BtnOK.Enabled = B
            Me.BtnCancel.Enabled = B
            Me.RBFather.Enabled = B
            Me.RBChild.Enabled = B
            Me.UTree.Enabled = Not B
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub FillText(ByVal ID As Integer)
        Try
            Dim Rows() As DataRow = mDataset.Tables(0).Select("ID=" & ID)
            Dim mDecrypt As New IDSecurity
            If Rows.Length > 0 Then
                For Each mRow As DataRow In Rows
                    Me.txtModeNo.Text = mRow("ModeNo") & ""
                    Me.txtModeName.Text = mRow("ModeName") & ""
                    Me.txtFormName.Text = mRow("ControlName") & ""
                    Me.txtFormPicture.Text = mDecrypt.DataDecrypt(mRow("ControlPicture").ToString.Replace("^", "'") & "")
                    Me.txtRemark.Text = mRow("Remark") & ""
                Next
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub GetToolEnable()
        Try
            With MainForm
                .ToolStripAdd.Enabled = AddEnable
                .ToolStripEdit.Enabled = EditEnable
                .ToolStripDelete.Enabled = DeleteEnable
                .ToolStripPrint.Enabled = PrintEnable
                .ToolStripImport.Enabled = ImportEnable
                .ToolStripExport.Enabled = ExportEnable
                .ToolStripFind.Enabled = FindEnable
                .ToolStripCheck.Enabled = CheckEnable
                .ToolStripUnCheck.Enabled = UncheckEnable
                .ToolStripComplete.Enabled = CompleteEnable
                .AddToolStripMenuItem.Enabled = .ToolStripAdd.Enabled
                .Edit1ToolStripMenuItem.Enabled = .ToolStripEdit.Enabled
                .DelToolStripMenuItem.Enabled = .ToolStripDelete.Enabled
                .SaveToolStripMenuItem.Enabled = .ToolStripSave.Enabled
                .CancelToolStripMenuItem.Enabled = .ToolStripCancel.Enabled
                .PrintToolStripMenuItem.Enabled = .ToolStripPrint.Enabled
                .ExportToolStripMenuItem.Enabled = .ToolStripImport.Enabled
                .ImportToolStripMenuItem.Enabled = .ToolStripExport.Enabled
                .FilterToolStripMenuItem.Enabled = .ToolStripFilter.Enabled
            End With

        Catch ex As Exception
            MsgBox("GetToolEnable:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub
#End Region


#Region "Control Events"

    Private Function UBSure_Click() As Boolean
        Try
            MainForm.ToolStripSave_Click(Nothing, Nothing)
        Catch ex As Exception
            MsgBox("UBSure_Click:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function


    Private Sub UTree_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles UTree.AfterSelect
        Try
            SelectNode = e.Node
            SetCtlEnable(False)
            If e.Action = TreeViewAction.ByKeyboard Or e.Action = TreeViewAction.Unknown Or e.Node.Tag.ToString = "All" Then
                bFind = False
            Else
                bFind = True
                ClearText()
                FillText(e.Node.Tag)
                MainForm.BtnEnable(bFind)
                GetToolEnable()
            End If
        Catch ex As Exception
            MsgBox("UTree_AfterSelect:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub


    Private Sub BtnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Try
            MainForm.ToolStripCancel_Click(Nothing, Nothing)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub BtnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnOK.Click
        MainForm.ToolStripSave_Click(Nothing, Nothing)
    End Sub

    Private Sub txtModeNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtModeNo.KeyDown, txtModeName.KeyDown, txtFormName.KeyDown, txtFormPicture.KeyDown, txtRemark.KeyDown
        Try
            If e.KeyCode = Keys.Shift Then
                e.Handled = True
                If e.KeyCode = Keys.Enter Then
                    SendKeys.Send("+{TAB}")
                    SendKeys.Send("{F4}")
                End If
            Else
                Select Case e.KeyCode
                    Case Keys.Enter
                        e.Handled = True
                        SendKeys.Send("{TAB}")
                        SendKeys.Send("{F4}")
                End Select
            End If
        Catch ex As Exception
            MsgBox("txtModeNo_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub Frm_SYSModeDefine_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            LoadUTree(Me.UTree)
            ClearText()
            SetCtlEnable(False)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

#End Region

End Class
